package util;

public class IntByteConverter {
	public static byte[] toByteArray(int source) throws Exception {
		byte[] result = new byte[4];
		for (int i = 0; i < 4; i++) {
			result[i] = (byte) ((source >> (8 * i)) & 0xFF);
		}
		return result;
	}

	public static int toInt(byte[] source) throws Exception {
		if (source.length != 4) {
			throw new Exception(source.length + " != 4");
		}
		int result = 0;
		for (int i = 0; i < 4; i++) {
			result += (source[i] & 0xFF) << (8 * i);
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		int xx = -3;
		System.out.println(toInt(toByteArray(xx)));
	}
}